using System.Windows;
using System.Windows.Controls;

namespace AvalonDock
{
    public abstract class PaneTabPanel : Panel
    {
        #region Constants and Fields

        /// <summary>
        /// TabItemStyle Dependency Property
        /// </summary>
        public static readonly DependencyProperty TabItemStyleProperty = DependencyProperty.Register(
            "TabItemStyle",
            typeof(Style),
            typeof(PaneTabPanel),
            new FrameworkPropertyMetadata(null, OnTabItemStyleChanged));

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets the TabItemStyle property.  This dependency property 
        /// indicates style to use for tabs.
        /// </summary>
        public Style TabItemStyle
        {
            get
            {
                return (Style)GetValue(TabItemStyleProperty);
            }
            set
            {
                SetValue(TabItemStyleProperty, value);
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Provides derived classes an opportunity to handle changes to the TabItemStyle property.
        /// </summary>
        protected virtual void OnTabItemStyleChanged(DependencyPropertyChangedEventArgs e)
        {
        }

        protected override void OnVisualChildrenChanged(DependencyObject visualAdded, DependencyObject visualRemoved)
        {
            base.OnVisualChildrenChanged(visualAdded, visualRemoved);

            var mc = visualAdded as ManagedContent;
            if (mc != null)
            {
                mc.Style = TabItemStyle;
                mc.ApplyTemplate();
            }
        }

        /// <summary>
        /// Handles changes to the TabItemStyle property.
        /// </summary>
        private static void OnTabItemStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((PaneTabPanel)d).OnTabItemStyleChanged(e);
        }

        #endregion
    }
}